package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @package com.leyou.item.web
 * @description: 操作后台商品品牌的controller层
 * @author: 许超越
 * @date 2019/6/25 14:52
 * @version: V1.0
 */
@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 后台商品品牌分页查询
     * @param: key 模糊查询的关键字
     * @param: page 当前页
     * @param: rows 一页显示多少行
     * @param: sortBy 排序字段
     * @param: desc 是否降序排序
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",defaultValue = "false") boolean desc
    ){
        return ResponseEntity.ok(brandService.queryBrandByPage(key, page, rows, sortBy, desc));
    }


    /**
     * 后台新增商品业务
     * @param: brandDTO 商品信息实体类，商品的部分信息封装类
     * @param: cids 请求参数中的分类的id集合
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {
        brandService.saveBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改品牌和类目
     * @param: brandDTO 品牌信息
     * @param: cids 品牌对应的类目
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {
        brandService.updateBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据商品分类查询品牌
     * @param: cid 商品分类id
     * @return
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCid(@RequestParam("id") Long cid) {
        return ResponseEntity.ok(brandService.queryBrandByCid(cid));
    }

    /**
     * 通过品牌id查询品牌
     * @param: id 品牌id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(brandService.queryById(id));
    }

    /**
     * 根据品牌id集合查询品牌
     * @param: idList 品牌分类id集合
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> idList) {
        return ResponseEntity.ok(brandService.queryBrandByIds(idList));
    }
}
